package com.esdk;

import cn.hutool.core.lang.caller.CallerUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.esdk.utils.*;

import java.util.Arrays;
import java.util.List;

final public class esdk{
	final public static class cst extends Constant{};
	final public static class tool extends EasyTool{};
	final public static class str extends EasyStr{};
	final public static class file extends EasyFile{};
	final public static EasyProp prop=new EasyProp("esdk.properties","application.properties");
	final public static class math extends EasyMath{};
	final public static class time extends EasyTime{};
	final public static class obj extends EasyObj{};
	final public static class array extends EasyArray{};
	final public static class excel extends EasyExcel{};
	final public static class reflect extends EasyReflect{};
	final public static class sql extends EasySql{};
	final public static class map extends EasyMap{};
	final public static class regex extends EasyRegex{};
	final public static class net extends EasyNet{};
	final public static class poi extends EasyPOI{};
	final public static class jdom extends EasyJdom{};
	final public static class bean extends EasyBean{};
	final public static class pinyin extends EasyPinyin{};
	final public static class json extends EasyJson{};
	final public static class http extends EasyHttp{};
	final public static class func extends EasyFunc{};
	final public static class enums extends EasyEnum{};
	final public static class query extends EasyQuery{};

	public static Log log(){
		return LogFactory.get(CallerUtil.getCallerCaller());
	}
	public static Log logger=LogFactory.get();

	public static void debug(Object template,Object... params){
		logger.debug(format(template,params,tool.InfoCallerTemplate));
	}
	public static void info(Object template,Object... params){
		logger.info(format(template,params,tool.InfoCallerTemplate));
	}

	public static void warn(Object template,Object... params){
		logger.warn(format(template,params,tool.ErrorCallerTemplate));
	}

	public static void error(Object template,Object... params){
		logger.error(format(template,params,tool.ErrorCallerTemplate));
	}

	public static void error(Exception e){
		logger.error(e);
	}


	public static void error(String errMsg,Exception e){
		logger.error(errMsg+" ExceptionCallStacks："+esdk.tool.getExceptionStackTrace(e));
	}

	/**sout为system.out.println的缩写，参考idea的快捷输入
	 * @param params：输出模板参数
	 * */
	public static void sout(Object template,Object... params) {
		System.out.println(esdk.time.getNowTime(time.FULL_FORMAT)+" "+format(template,params,esdk.tool.InfoCallerTemplate));
	}

	/**
	 * 可以在idea打印黄色日志
	 * @param params：日志内容串接在一起
	 * */
	public static void swarn(Object template,Object... params) {
		String content=esdk.time.getNowTime(time.FULL_FORMAT)+" "+format(template,params,esdk.tool.InfoCallerTemplate);
		System.out.println(Constant.YELLOW_HIGH+content+Constant.RESET);
	}

	/**serr为system.err.println的缩写，参考idea的快捷输入
	 * @param params：日志内容串接在一起
	 * */
	public static void serr(Object template,Object... params) {
		System.err.println(esdk.time.getNowTime(time.FULL_FORMAT)+" "+format(template,params,esdk.tool.ErrorCallerTemplate));
	}

	private static String format(Object template,Object[] params,String callerTemplate) {
		String prefix=esdk.str.addEnd(esdk.tool.getCaller(callerTemplate)," ");
		return prefix+format(template,params);
	}

	private static String format(Object template,Object[] params){
		String content;
		if(template instanceof CharSequence && params.length>0){
			if(esdk.regex.find(template.toString(),"\\{\\d{0,2}\\}"))
				content=str.format(template.toString(),params);
			else
				content=template+array.toStr(params,"");
		}else
			content=template+"";
		return content;
	}

	/**返回数组，简化打字，确保在调试时返回的不是Object[]而是T[]*/
  public static <T>T[] toArr(T...array){
		return esdk.array.toArray(Arrays.asList(array));
	}
	/**返回数组，简化打字*/
	public static <T>T[] to(T...array){
		return toArr(array);
	}
	/**返回数组，简化打字*/
	public static <T>T[] var(T...array){
		return toArr(array);
	}

	public static EasyQuery getEasyQuery(){
		return new EasyQuery();
	}

	public static <T extends Comparable> EasyQuery<T> getEasyQuery(List<T> list){
		return new EasyQuery<T>((List)list);
	}

	public static void timing(String topic, Runnable runnable){
		long startTime = System.currentTimeMillis();
		runnable.run();
		long endTime = System.currentTimeMillis();
		sout(topic + "用时：" + (endTime - startTime) + "ms");
	}

	public static void main(String[] args){
		esdk.sout(esdk.toArr("a","b").getClass());
		esdk.tool.assertEquals(prop.getBoolean("IsShowSql"),true);
		esdk.tool.assertEquals(prop.getString("redis.host"),"redis.server");
		esdk.tool.assertEquals(prop.getString("testempty"),null);
		esdk.tool.assertEquals(esdk.map.createMap("{name=李元富,sex=男}").get("name"),"李元富");
		esdk.tool.assertEquals(esdk.map.createMap("name","张三","code","zhang","sex","男","age",18).toString(),"{name=张三, code=zhang, sex=男, age=18}");
		esdk.tool.printUpTime();
		esdk.tool.assertEquals(esdk.to(1,2,4).length,3);
		esdk.tool.assertEquals(esdk.var(1,2,4).length,3);
		esdk.tool.assertEquals(esdk.toArr(1,2,4).length,3);
	}


}

